<?php 
namespace app\api\controller;
use think\Controller;
use think\Db;
/**
 供应链商品相关
 */
class Operators extends Common
{

	//首页待领取优惠券
    protected  $page = 10;
    private $uid = '';
    private $useinfo = '';
    public function __construct()
    {
        parent::__construct();
        $this->uid = $this->u_id;
      //判断下运营商权限？ 有没
        $userinfo = db('admin_user')->field('province,city,role,area,yunying_flag,area_ids,province_id,money')->where(['id'=>$this->uid])->find();
        if($userinfo['role'] != '4' && $userinfo['role'] != '5'){
            return $this->ajaxJson(202,'权限不足！');
        }
        $this->userinfo = $userinfo;
    }
    
    //运营商呢 需要获取到所有的批发商
    public function get_wholesaler(){
        //根据用户id获取呢
        $uid = $this->uid;
        $limit = $this->getlimit();
        $data = input('post.');
        if(!empty($data['shopname'])){
            $where[] = ['a.shopname', 'like',"%".trim($data['shopname']).'%'];
        }
        //我所在的位置和权限
        $userinfo = $this->userinfo;
        //如果省代理
        $where[] = ['role', 'in', [2,3]];
        $where[] = ['province','=',$userinfo['province']];
        if($userinfo['role'] == 5){
            $where[] = ['area_id','in',$userinfo['area_ids']];    //城市对应 县区包含呢
        }

        $url = $this->geturl();
        $admin = db('admin_user a')
            ->field('a.id uid,shopname,mobile,concat(province,city,area) address,concat("'.$url.'",b.path) icon,c.name')
            ->join('admin_attachment b','b.id=a.shoplogo','left')
            ->join('admin_role c','c.id=a.role','left')
            ->where($where)
            ->order('a.id desc')
            ->limit($limit)->select();
        if(empty($admin)){//201
            return $this->ajaxJson(201,'没有数据！');
        }
        $count =db('admin_user a')
            ->field('shopname,mobile,concat(province,city,area) address,concat("'.$url.'",b.path) icon,c.name')
            ->join('admin_attachment b','b.id=a.shoplogo','left')
            ->join('admin_role c','b.id=a.role','left')
            ->where(['a.id'=>$uid])->count();

        return $this->ajaxJson(200,'获取成功！',['totalnum'=>$count,'list'=>$admin]);
    }

    //该批发商下边的所有产品
    public function get_agent_pro(){
        //批发商？
        $input = input('post.');
        if(!empty($input['xuid'])){
            $this->uid = $input['xuid'];
        }
        $pifqashang = db('admin_user')->where(['id'=>$this->uid])->find();
        if(empty($pifqashang)){
            return $this->ajaxJson(202,'没有该权限哦！');
        }
        $data = input('post.');
        if(empty($data['uid'])){
            return $this->ajaxJson(202,'未找到该批发商！');
        }
        $admin_id =intval($data['uid']);

        if(!empty($data['proname'])){
            $where[] = ['c.name', 'like',"%".trim($data['proname']).'%'];
        }
        //传过来的代理商id？uid  可直接查询
        $where[] = ['a.admin_id','=',$admin_id];
        if(!empty($data['type'])){
            $where[] = ['a.is_forbid','=',intval($data['type'])];
        }

        $limit = $this->getlimit();
        //查出所有产品 且是否禁止购需要设置  算了 直接显示吧
        $url = $this->geturl();
        $admin = db('admin_goods a')
            ->field('a.is_forbid,a.id agent_goods_id,min(price) price,concat("'.$url.'",b.path) image,c.name pro_name')
            ->join('goods c','c.id=a.good_id','left')
            ->join('admingoods_sku d','d.admingood_id=a.id','left')
            ->join('admin_attachment b','b.id=c.image','left')
            ->where($where)
            ->group('a.good_id')
            ->order('a.id desc')
            ->limit($limit)->select();

        if(empty($admin)){//201
            return $this->ajaxJson(201,'没有数据！');
        }
        $count =  db('admin_goods a')
            ->field('a.id agent_goods_id,min(price) price,concat("'.$url.'",b.path) image,c.name pro_name')
            ->join('goods c','c.id=a.good_id','left')
            ->join('admingoods_sku d','d.admingood_id=a.id','left')
            ->join('admin_attachment b','b.id=c.image','left')
            ->group('a.good_id')
            ->where($where)->count();
        return $this->ajaxJson(200,'获取成功！',['totalnum'=>$count,'list'=>$admin]);
    }

    //开启和取消禁购买
    public function jingou(){
        //禁购的话 1需要是运营商身份 2咳咳 一个地区？直接进购和取消就好了
        $data = input('post.');
        if(empty($data['agent_goods_id'])){
            return $this->ajaxJson(202,'参数错误！');
        }
        $type = intval($data['type']);
        $agent_goods_id = intval($data['agent_goods_id']);
        $is_forbid = db('admin_goods')->field('is_forbid')->where(['id'=>$agent_goods_id])->find();
        if(empty($is_forbid)){
            return $this->ajaxJson(202,'非法操作！');
        }
        if($type == $is_forbid['is_forbid']){
            return $this->ajaxJson(202,'状态已更改！');
        }
        db('admin_goods')->where(['id'=>$agent_goods_id])->setField(['is_forbid'=>$type]);
        return $this->ajaxJson(200,'操作成功！');
    }

    //添加门店
    public function add_shop(){
        //门店名称
        $data = input('post.');
        //添加门店？
        if(empty($data['shop_name']) || empty($data['shop_type'])
            || empty($data['user_name'])  || empty($data['user_phone'])
            || empty($data['address'])  || empty($data['license'])
            || empty($data['shop_pics'])  || empty($data['shop_desc'])
            || empty($data['province'])  || empty($data['city'])
            || empty($data['area'])
        ){
            return $this->ajaxJson(202,'参数错误！');
        }
        //查出城市id和地区所在id
        $cityid = db('china')->where('name','like','%'.$data['city'].'%')->value('id');
        $areaid = db('china')->where('name','like','%'.$data['area'].'%')->where('pid','=',$cityid)->value('id');
        //需要根据
        //需要判断和关联   is_auth
        $shoparr = db('shop')->where(['user_phone'=>$data['user_phone'],'del'=>0])->count();
        if($shoparr > 0){
            return $this->ajaxJson(202,'该用户已经申请过门店了！');
        }
        //做对应的记录
        //首先 不能重复门店吧
        $data['yuid'] = $this->uid;
        $data['shop_pics'] = trim($data['shop_pics'],',');
        //这个用户在系统中存在否
        $user_id = db('admin_user')->where(['mobile'=>$data['user_phone']])->value('id');
        if(empty($user_id)){
            //添加一下呢？
            $useradd = [];
            $useradd['mobile'] = $data['user_phone'];
            $user_id  = db('admin_user')->insertGetId($useradd);
        }
        $data['uid'] = $user_id;
        $data['area_id'] = $areaid;
        //执行添加
        db('shop')->field('uid,area,city,province,shop_desc,shop_pics,license,address,user_phone,user_name,shop_type,shop_name,yuid,area_id')->insert($data);
        return $this->ajaxJson(200,'操作成功!');
    }

    //获取我添加的门店？
    public function get_myshop(){
        //我添加的
        $data = input('post.');

        $limit = $this->getlimit();
        //查出所有产品 且是否禁止购需要设置  算了 直接显示吧
        $where[] = ['del','=','0'];
        if(!empty($data['type'])){
            if($data['type'] == '1'){
                $where[] = ['flag','=','0'];
            }
        }
        //我管辖区域的？
        //判断我的权限
        $userinfo = $this->userinfo;
        if($userinfo['role'] == '4'){//省的
            $where[] = ['province','=',$userinfo['province']];
        }else{
            //县区有没有包含的
            $where[] = ['area_id','in',$userinfo['area_ids']];
        }
        //省市区查询？
        $data = input('post.');
        if(!empty($data['province'])){
            $where[] = ['province','=',$data['province']];
        }
        if(!empty($data['city'])){
            $where[] = ['city','=',$data['city']];
        }
        if(!empty($data['area'])){
            $where[] = ['area','=',$data['area']];
        }
        $url = $this->geturl();
        $admin = db('shop a')
            ->field('a.shop_name,a.flag,a.uid shop_id,shop_type,concat("'.$url.'",b.path) shop_pics,a.user_name,a.user_phone,concat(a.province,a.city,a.area,a.address) address')
            ->join('admin_attachment b','b.id=a.shop_pics','left')
            ->where($where)
            ->order('a.id desc')
            ->limit($limit)->select();

        if(empty($admin)){//201
            return $this->ajaxJson(201,'没有数据！');
        }
        $count =  db('shop a')
            ->field('a.shop_name,a.id shop_id,shop_type,concat("'.$url.'",b.path) shop_pics,a.user_name,a.user_phone,concat(a.province,a.city,a.area,a.address) address')
            ->join('admin_attachment b','b.id=a.shop_pics','left')
            ->where($where)->count();
        return $this->ajaxJson(200,'获取成功！',['totalnum'=>$count,'list'=>$admin]);
    }

    //需要一个获取门店详情的
    public function get_shop_detail(){
        $data = input('post.');
        $shop_id = intval($data['shop_id']);
        $url = $this->geturl();
        $admin = db('shop a')
            ->field('a.shop_name,a.flag,a.shop_pics,a.id shop_id,shop_type,concat("'.$url.'",b.path) license,a.user_name,a.user_phone,a.province,a.city,a.area,a.address')
            ->join('admin_attachment b','b.id=a.license','left')
            ->where(['a.uid'=>$shop_id])
            ->find();
        if(empty($admin)){
            return $this->ajaxJson(201,'没有数据！');
        }
        //他的 多图呢？
        $admin['shop_pics_list'] = array();

        foreach (explode(',',$admin['shop_pics']) as $k=>$v){
            $z = array();
            $z['id'] = $v;
            $z['path'] = $url.get_file_path($v);
            $admin['shop_pics_list'][] = $z;
        }
        return $this->ajaxJson(200,'获取成功！',['info'=>$admin]);
    }

    //获取我所管辖的省市区 用于搜索
//    public function get_my_area(){
//        $data = input('post.');
//        $type = intval($data['type']);
//        $userinfo = $this->userinfo;
//        //获得我所在的省？
//        //首先获取省份
//
//
//        if($userinfo['role'] == '4'){
//
//
//        }else{
//
//        }
//
//        //省市区 都要获取呢？
//    }

    //财务管理
    //获取财务相关的信息
    public function get_home(){
        //获取订单数据 来统计
        //权限划分呢
        $userinfo = $this->userinfo;
        $data['money'] = $userinfo['money'];
        if($userinfo['role'] == 4){
            //省代理的呢
            $filed = 'sum((a.pay_money-a.refund_money)*u.plat*u.pro_plat) money';
            //需要是一个省份的
            $where[] = ['a.pro_operator','=',$this->uid];
            $agent_role = '省代理';
        }else{
            //区县代理
            $filed = 'sum((a.pay_money-a.refund_money)*u.plat*u.area_plat) money';
            $where[] = ['a.area_operator','=',$this->uid];
            $agent_role = '区县代理';
        }
        $where[] = ['a.status','in','1,2'];
        //我的权限决定查询的东西
        $order = db('order a')
                    ->field($filed)
                    ->join('admin_user u','u.id=a.shop_id','left')
                    ->where($where)
                    ->find();
        $data['dongjie_money'] = empty($order)?'0.00':sprintf("%01.2f",$order['money']/10000);
        //今天的交易金额？
        $where[] = ['addtime','>',strtotime(date('Y-m-d'))];
        $data['newday_money'] = db('order a')
            ->field($filed)
            ->where($where)
            ->sum('a.pay_money')-db('order a')
                ->field($filed)
                ->where($where)
                ->sum('a.refund_money');
        //用户的名 和 权限？
        $data['str'] = '欢迎'.urldecode(db('user')->where(['id'=>$this->uid])->value('nickname')).'回来，您当前为'.$agent_role;
        return $this->ajaxJson(200,'获取成功！',$data);
    }

    //收入的数据
    public function revenue_record(){
        $userinfo = $this->userinfo;
        $input = input('post.');
        if(!empty($input['xuid'])){
            $this->uid = $input['xuid'];
            $userinfo = db('admin_user')->field('province,city,role,area,yunying_flag,area_ids,province_id,money')->where(['id'=>$this->uid])->find();
        }

        $data['money'] = $userinfo['money'];
        if($userinfo['role'] == 4){
            //省代理的呢
            $filed = 'sum((a.pay_money-a.refund_money)*u.plat*u.pro_plat) money';
            //需要是一个省份的
            $where[] = ['a.pro_operator','=',$this->uid];
        }else{
            //区县代理
            $filed = 'sum((a.pay_money-a.refund_money)*u.plat*u.pro_plat) money';
            $where[] = ['a.area_operator','=',$this->uid];
        }
        $historywhere = $historynumwhere = $where;
        $where[] = ['a.status','in','1,2'];
        //我的权限决定查询的东西
        $order = db('order a')
            ->field($filed)
            ->join('admin_user u','u.id=a.shop_id','left')
            ->where($where)
            ->find();
        $data['dongjie_money'] = empty($order)?'0.00':sprintf("%01.2f",$order['money']/10000);
        //今天的交易金额？
        $where[] = ['addtime','>',strtotime(date('Y-m-d'))];
        $data['newday_money'] = db('order a')
                ->field($filed)
                ->where($where)
                ->sum('a.pay_money')-db('order a')
                ->field($filed)
                ->where($where)
                ->sum('a.refund_money');
        //查 历史佣金 今日订单？ 历史订单

        $historywhere[] = ['a.status','>','2'];
        $history = db('order a')
            ->field($filed)
            ->join('admin_user u','u.id=a.shop_id','left')
            ->where($historywhere)
            ->find();
        $data['history_money'] = empty($history)?'0.00':round($history['money']/10000,2);

        //今日订单
        $data['newday_num'] = db('order a')
                ->where($where)
                ->count();
        //历史订单
        $data['history_num'] = db('order a')
            ->where($historynumwhere)
            ->count();
        //返回前10条的记录数据 单号 金额 收益到账时间 收益
        $data['list'] = db('order a')
            ->field($filed.',(pay_money-refund_money) order_money,FROM_UNIXTIME(receivetime,"%Y-%m-%d %H:%i") receivetime,order_num,a.id')
            ->join('admin_user u','u.id=a.shop_id','left')
            ->where($historywhere)->order('a.id desc')->group('a.id')->limit('0,10')->select();
        return $this->ajaxJson(200,'获取成功！',$data);
    }

    //查看更多的呢？
    public function get_revenue_record(){
        $input = input('post.');
        $userinfo = $this->userinfo;
        if(!empty($input['xuid'])){
            $this->uid = $input['xuid'];
            $userinfo = db('admin_user')->field('province,city,role,area,yunying_flag,area_ids,province_id,money')->where(['id'=>$this->uid])->find();
        }
        if($userinfo['role'] == 4){
            //省代理的呢
            $filed = 'sum((a.pay_money-a.refund_money)*u.plat*u.pro_plat) money';
            //需要是一个省份的
            $where[] = ['a.pro_operator','=',$this->uid];
        }else{
            //区县代理
            $filed = 'sum((a.pay_money-a.refund_money)*u.plat*u.pro_plat) money';
            $where[] = ['a.area_operator','=',$this->uid];
        }

        //条件
        if(!empty($input['star_time'])){
            $strt  = $input['star_time'];
            $end  = $input['end_time'];
            $where[] = ['a.receivetime','between',[strtotime($strt),strtotime($end.' 23:59:59')]];
        }

        $limit = $this->getlimit();
        $where[] = ['a.status','>','2'];
        //查出所有产品 且是否禁止购需要设置  算了 直接显示吧

        $data = db('order a')
            ->field($filed.',(pay_money-refund_money) order_money,FROM_UNIXTIME(receivetime,"%Y-%m-%d %H:%i") receivetime,order_num,a.id')
            ->join('admin_user u','u.id=a.shop_id','left')
            ->where($where)->order('a.id desc')->group('a.id')
            ->order('a.id desc')
            ->limit($limit)->select();

        $count =  db('order a')
            ->field($filed.',(pay_money-refund_money) order_money,FROM_UNIXTIME(receivetime,"%Y-%m-%d %H:%i") receivetime,order_num,a.id')
            ->join('admin_user u','u.id=a.shop_id','left')
            ->where($where)->order('a.id desc')->group('a.id')
            ->count();
        //还要一个总收益
        return $this->ajaxJson(200,'获取成功！',['totalnum'=>$count,'money'=>$userinfo['money'],'list'=>$data]);

    }



    //订单查看
    public function order_sel(){
        //查出我所有下级的订单？
        $input = input('post.');
        $userinfo = $this->userinfo;
        if(!empty($input['xuid'])){
            $this->uid = $input['xuid'];
            $userinfo = db('admin_user')->field('province,city,role,area,yunying_flag,area_ids,province_id,money')->where(['id'=>$this->uid])->find();
        }

        if($userinfo['role'] == 4){
            //省代理的呢
            //需要是一个省份的
            $where[] = ['a.pro_operator','=',$this->uid];
        }else{
            //区县代理
            $where[] = ['a.area_operator','=',$this->uid];
        }

        //条件
        if(!empty($input['star_time'])){
            $strt  = $input['star_time'];
            $end  = $input['end_time'];
            $where[] = ['a.paytime','between',[strtotime($strt),strtotime($end.' 23:59:59')]];
        }
        $limit = $this->getlimit();
        $where[] = ['a.status','>','0'];
        if(!empty($input['status'])){
            $status  = $input['status'];
            if($status>4){
                $where[] = ['b.type','>',0];
            }else{
                $where[] = ['a.status','=',$status];
            }
        }
        //查出所有产品 且是否禁止购需要设置  算了 直接显示吧
        $url = $this->geturl();
        //先查出一个产品的吧

        $data =  db('order_detail b')
            ->field('b.type,a.id oid,a.status,a.order_num,(pay_money-refund_money) order_money,concat("'.$url.'",e.path) icon,d.name pro_name,num cnt,b.id dea_id')
            ->join('order a','b.order_num=a.order_num','left')
            ->join('admingoods_sku f','f.id=b.sku_id','left')
            ->join('goods_sku c','c.id=f.sku_id','left')
            ->join('goods d','d.id=c.good_id','left')
            ->join('admin_attachment e','e.id=d.image','left')
            ->where($where)->order('a.id desc')
            ->order('b.id desc')
            ->group('b.id')
            ->limit($limit)->select();
        //0待付款，1待发货，2待收货，3待评价 4 已完成 5申请退款6退货
        foreach ($data as $k=>$v){
            if($v['type']>0){
                $data[$k]['status'] = $v['type']=='1'?5:6;
            }
        }

        //我需要对产品
        if(empty($data)){//201
            return $this->ajaxJson(201,'没有数据！');
        }
        $count =  db('order_detail b')
            ->field('a.id oid,a.status,a.order_num,(pay_money-refund_money) order_money,group_concat(concat("'.$url.'",e.path)) icon,group_concat(d.name)pro_name,sum(num)cnt')
            ->join('order a','b.order_num=a.order_num','left')
            ->join('admingoods_sku f','f.id=b.sku_id','left')
            ->join('goods_sku c','c.id=f.sku_id','left')
            ->join('goods d','d.id=c.good_id','left')
            ->join('admin_attachment e','e.id=d.image','left')
            ->where($where)->order('b.id desc')
            ->group('b.id')
            ->count();
        //还要一个总收益
        return $this->ajaxJson(200,'获取成功！',['totalnum'=>$count,'money'=>$userinfo['money'],'list'=>$data]);
    }

    public function add_chi_agent(){
        //我都能添加哪个市的？
        //返回下我管辖的区域吧
        //只需哪个省的就行了吧？
        //我所在的省份？
        $data = input('post.');
        //只能添加本省的呢？
        $userinfo = $this->userinfo;
        if($userinfo['role'] != 4){
            return $this->ajaxJson(202,'只有省代才能可以！');
        }
        //该联系人有没有被使用
        $usercnt = db('admin_user')->where(['mobile'=>$data['mobile']])->count();
        if($usercnt>1){
            return $this->ajaxJson(202,'该手机号已被使用！');
        }
        //首先 添加的肯定是 市或区的 直接传来 当前城市名称  和 区名称的组合
        $cityarr = db('china')->field('id,pid,name')->where(['name'=>$data['city']])->find();
        if(empty($cityarr)){
            return $this->ajaxJson(202,'未找到该城市！');
        }
        $city_id = $cityarr['id'];
        if($cityarr['pid'] != $userinfo['province_id']){
            return $this->ajaxJson(202,'只能选择'.$userinfo['province'].'的城市！');
        }
        //字符串处理下
        $area = explode(',',$data['area']);
        //括号？
        $areastr = "'".implode("','",$area )."'";
        //然后 搜下所属区域吧
        $areaarr = db('china')->field('name,id')->where("pid=$city_id and name in ($areastr)")->select();
        //要添加的这个区
        //区县有没有被搞掉呢？
        $arrcheck = [];
        $arearr = [];
        foreach ($areaarr as $k=>$v){
            if(empty($arrcheck[$v['name']]))
            //直接用id吧
            $id = $v['id'];
            $areacount = db('admin_user')->where("role=5 and FIND_IN_SET($id,area_ids)")->count();
            if($areacount>0){
                return $this->ajaxJson(202,$v['name'].' 已被别的运营商授权！');
            }
            $arearr[] = $v['id'];
            $arrcheck[$v['name']] = 1;
        }
        //检测
        foreach ($area as $k=>$v){
            if(empty($arrcheck[$v])){
                return $this->ajaxJson(202,$v.' 未找到！');
            }
        }
        //执行添加
        $adddata = [];
        $adddata['province_id'] = $userinfo['province_id'];
        $adddata['city_id'] = $city_id;
        $adddata['area_ids'] = implode(',',$arearr);
        $adddata['area_names'] = $data['area'];
        $adddata['role'] = 5;
        $adddata['create_time'] = time();
        $adddata['yunying_flag'] = 0;
        $adddata['yunying_id1'] = $data['yunying_id1'];
        $adddata['yunying_id2'] = $data['yunying_id2'];
        $adddata['yunying_les'] = $data['yunying_les'];;
        $adddata['yunying_address'] = $data['yunying_address'];;
        $adddata['mobile'] = $data['mobile'];//
        $adddata['nickname'] = $data['nickname'];//
        db('admin_user')->insert($adddata);

        return $this->ajaxJson(200,'操作成功 ！');
    }

    //如果呀 他是省运营 那么是可以查到下级运营商的
    public function get_chi_agent(){
        //所有运营商的呢？
        //查出我这个省 下边的所有线运营
        $userinfo = $this->userinfo;
        if($userinfo['role'] != 4){
            return $this->ajaxJson(202,'权限不足！');
        }
        $limit = $this->getlimit();
        $data = input('post.');
        $where = ['role'=>5,'province_id'=>$userinfo['province_id']];
        if(!empty($data['yunying_flag'])){
            $where['yunying_flag'] = $data['yunying_flag']-1;
        }

        //province_id 相同的运营商
        //我下级所有的运营商呢？
        $userlist = db('admin_user')
            ->field('id xuid,nickname,mobile,yunying_address,(yunying_flag+1)yunying_flag')
            ->where($where)
            ->limit($limit)
            ->select();
        if(empty($userlist)){
            return $this->ajaxJson(201,'没有数据！');
        }

        $count = db('admin_user')
            ->where($where)
            ->count();
        //还要一个总收益
        return $this->ajaxJson(200,'获取成功！',['totalnum'=>$count,'list'=>$userlist]);

    }

    //下级运营商的首页嗯？
    public function get_chi_home(){
        //获取订单数据 来统计
        //权限划分呢
        $inputdata = input('post.');
        if(empty($inputdata['xuid'])){
            return $this->ajaxJson(202,'参数错误！');
        }
        $xuid  = $inputdata['xuid'];
        $data = db('admin_user')->field('mobile,yunying_address,money,area_names,nickname')->where(['id'=>$xuid])->find();
        if(empty($data)){
            return $this->ajaxJson(202,'非法操作！');
        }
        $data['nickname'] = urldecode($data['nickname']);
            //区县代理
        $filed = 'sum((a.pay_money-a.refund_money)*u.plat*u.pro_plat) money';
        $where[] = ['a.area_operator','=',$xuid];
        $where[] = ['a.status','in','1,2'];
        //我的权限决定查询的东西
        $order = db('order a')
            ->field($filed)
            ->join('admin_user u','u.id=a.shop_id','left')
            ->where($where)
            ->find();
        $data['dongjie_money'] = empty($order)?'0.00':sprintf("%01.2f",$order['money']/10000);
        //今天的交易金额？
        $where[] = ['addtime','>',strtotime(date('Y-m-d'))];
        $data['newday_money'] = db('order a')
                ->field($filed)
                ->where($where)
                ->sum('a.pay_money')-db('order a')
                ->field($filed)
                ->where($where)
                ->sum('a.refund_money');
        //用户的名 和 权限？
        return $this->ajaxJson(200,'获取成功！',$data);
    }

    //一个月收入统计
    public function get_income(){
        //时间按周记  钱按10分记

        // 4周？ 7天递减吧
        $userinfo = $this->userinfo;
        $input = input('post.');
        if(!empty($input['xuid'])){
            $this->uid = $input['xuid'];
            $userinfo = db('admin_user')->field('province,city,role,area,yunying_flag,area_ids,province_id,money')->where(['id'=>$this->uid])->find();
        }

        $data['money'] = $userinfo['money'];
        if($userinfo['role'] == 4){
            //省代理的呢
            $filed = 'sum((a.pay_money-a.refund_money)*u.plat*u.pro_plat) money';
            //需要是一个省份的
            $where[] = ['a.pro_operator','=',$this->uid];
        }else{
            //区县代理
            $filed = 'sum((a.pay_money-a.refund_money)*u.plat*u.pro_plat) money';
            $where[] = ['a.area_operator','=',$this->uid];
        }
        $where[] = ['status','>',2];
        $history = db('order a')
            ->field($filed)
            ->join('admin_user u','u.id=a.shop_id','left')
            ->where($where)
            ->find();
        //现在 我需要知道 每天七天的
        $data[] = empty($history)?'0.00':round($history['money']/10000,2);
    }


    //积分产品兑换接口
    public function duihuanpro(){
        $data = input('post.');
        if(empty($data['num']) || empty($data['iid']) || empty($data['aid'])){
            return $this->ajaxJson(201,'参数错误！');
        }
        $cid = intval($data['iid']);
        $aid = intval($data['aid']);
        $pro = db('integralmall')->where(['id'=>$cid])->find();
        if(empty($pro)){
            return $this->ajaxJson(201,'非法操作！');
        }
        $num = intval($data['num']);
        $score =db('admin_user')->where(['id'=>$this->uid])->value('score');

        $accout = $num*$pro['score'];
        if($score<$accout){
            return $this->ajaxJson(201,'积分不足！');
        }

        $address = db('address')->field('province,city,area,address')->where(['id'=>$aid,'u_id'=>$this->uid])->find();
        if(empty($address)){
            return $this->ajaxJson(201,'非法操作！');
        }

        $z = [];
        $z['uid'] = $this->uid;
        $z['iid'] = $cid;
        $z['aid'] = $aid;
        $z['provitice'] = $address['province'];
        $z['city'] = $address['city'];
        $z['area'] = $address['area'];
        $z['address'] = $address['address'];
        $z['addtime'] = date('Y-m-d H:i:s');
        db('score_pro')->insert($z);
        //日志

        $z = [];
        $z['u_id'] = $this->uid;
        $z['type'] = 7;
        $z['score'] = '-'.$accout;
        $z['addtime'] = time();
        db('score_record')->insert($z);
        //减积分
        db('admin_user')->where(['id'=>$this->uid])->setDec('score',$accout);
        return $this->ajaxJson(200,'操作成功！');
    }

    protected function geturl(){
        return  '';
        return  'http://'.$_SERVER['SERVER_NAME'];
    }

    //财务管理
    protected function getlimit($yy=0){
        $yy = input('post.pagenum')?input('post.pagenum'):10;
        $page = input('page');
        $pagess = empty($page)?'1':$page;
        $limit = ($pagess-1)*$yy.','.$yy;
        return $limit;
    }


}